﻿using brand.admin.cyt369.com.services.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;

namespace brand.admin.cyt369.com.services.Services
{
    public class HotBrandService: admin.cyt369.com.services.ORM.BaseService<HotBrandService>
    {
        /// <summary>
        /// 获取热门品牌
        /// </summary>
        /// <returns></returns>
        public IQueryable<brand.admin.cyt369.com.services.Models.Brand> GetsHotBrand(int page, int rows, ref int total,
                                                                                 int? status = null, string name = null,
                                                                                 int? year = null, int? month = null,
                                                                                 int? categoryID = null, bool isDeleted = false)
        {
            using (var ctx = this.Ctx)
            {
                var items = ctx.Brands.Include(b => b.HotBrand).Include(b => b.BrandImages)
                    .Where(b => b.HotBrand != null)
                    .OrderBy(b => -b.HotBrand.OrderNumber).AsQueryable();
                if (status.HasValue)
                {
                    items = items.Where(c => c.ReviewResult == status.Value);
                }

                if (year.HasValue)
                {
                    items = items.Where(c => c.CreateDate.Year == year.Value);
                }

                if (month.HasValue)
                {
                    items = items.Where(c => c.CreateDate.Month == month.Value);
                }

                if (categoryID.HasValue)
                {
                    items = items.Where(i => i.CategoryID == categoryID);
                }

                if (!string.IsNullOrEmpty(name))
                {
                    items = items.Where(c => c.Name.Contains(name));
                }

                return items = this.Pagination(page, rows, ref total, items, c => c.Id).ToList().AsQueryable();
            }
        }

        /// <summary>
        /// 获取非热门品牌
        /// </summary>
        /// <returns></returns>
        public IQueryable<brand.admin.cyt369.com.services.Models.Brand> GetsNoHotBrand()
        {
            using (var ctx = this.Ctx)
            {
                var brands = ctx.Brands.Include(b => b.HotBrand).Include(b=>b.BrandImages)
                    .Where(b => b.HotBrand == null);
                return brands.ToList().AsQueryable();
            }
        }


        /// <summary>
        /// 删除热门品牌
        /// </summary>
        /// <param name="hotBrandId"></param>
        /// <returns></returns>
        public bool HotBrandDelete(long hotBrandId)
        {
            using (var ctx = this.Ctx)
            {
                var brand = (from o in ctx.HotBrands
                             where o.Id == hotBrandId
                             select o).FirstOrDefault();

                if (brand != null)
                {
                    ctx.HotBrands.Remove(brand);
                    ctx.SaveChanges();

                    return true;
                }
            }

            return false;
        }

    }
}
